<?php

class Obj_DBTable_Countries extends Zend_Db_Table_Abstract
{
    protected $_name = 'countries';
    protected $_primary = 'ID';

    
    public function getCountries($activeOnly) {
    	$select = $this->select()->from($this)->order('ID')->order('Name');
    	if ($activeOnly === true) $select->where('active = 1');
    	return $this->fetchAll($select)->toArray();
    }
    
    public function getFullCountryList($activeOnly) {
    	$select = $this->select()->from($this, array('CountryID' => 'ID', 'CountryName' => 'Name', 'CountryAlias' => 'Alias'))->order('countries.ID');
    	if ($activeOnly === true) $select->where('countries.active = 1');
    	
    	$select->join('regions', 'countries.ID = regions.CountryID AND regions.active = 1', array('RegionID' => 'ID', 'RegionName' => 'Name', 'RegionAlias' => 'alias'));
    	$select->join('cities', 'regions.ID = cities.RegionID AND cities.active = 1', array('CityID' => 'ID', 'CityName' => 'Name', 'CityAlias' => 'alias'));
    	$select->join('locations', 'cities.ID = locations.CityID AND locations.active = 1', array('LocationID' => 'ID', 'LocationName' => 'Name', 'LocationAlias' => 'alias'));
    	
    	$select->setIntegrityCheck(false);    	
    	return $this->fetchAll($select)->toArray();
    }
    
    public function getExtendedCountries() {
    	$select = $this->select()->from($this, array('ID', 'Name', 'smallFlag', 'alias', 'indexX', 'indexY', 'staticX', 'staticY', 'description'))->order('countries.Name');
    	$select->where('countries.active = 1');
    	$select->joinLeft('objects', 'countries.ID = objects.CountryID AND objects.Active = 1', array('objc' => new Zend_Db_Expr('COUNT(objects.ID)'), 'Attribute'));
    	$select->group('objects.Attribute');
    	$select->group('countries.ID');
    	$select->setIntegrityCheck(false);
    	return $this->fetchAll($select)->toArray();
    }
     
    public function getCountry($id) {
    	$select = $this->select()->from($this)->where('ID = ?', (int) $id);
    	$res = $this->fetchRow($select);
    	if ($res) {
    		return $res->toArray();
    	}
    	return false;
    }
     
    public function getAliasCountry($alias) {
    	$select = $this->select()->from($this)->where('alias = ?', trim($alias));
    	$res = $this->fetchRow($select);
    	if ($res) {
    		return $res->toArray();
    	}
    	return false;
    } 
}

